<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

    <!-- 通用设置 -->
    <#if baseColumnList>
        <!-- 通用查询列 -->
        <sql id="Base_Column_List">
            <#list table.commonFields as field>
                ${field.name},
            </#list>
            ${table.fieldNames}
        </sql>

        <!-- 通用查询列 -->
        <sql id="Base_Column_List_Insert">
        <#list table.fields as field>
            ${r"#{info."}${field.propertyName}${r"}"},
        </#list>
        </sql>

        <!-- 通用条件列 -->
        <sql id="${entity}ByCondition">
            <#list table.fields as field>
                <#if !field.keyFlag>
                    <#if (field.propertyType!"") == "Date">
                        <if test="${field.propertyName}!=null">
                            AND ${field.name} = ${r"#{"}${field.propertyName}${r"}"}
                        </if>
                    </#if>
                    <#if (field.propertyType!"") != "Date">
                        <if test="${field.propertyName}!=null and ${field.propertyName}!=''">
                            AND ${field.name} = ${r"#{"}${field.propertyName}${r"}"}
                        </if>
                    </#if>
                </#if>
            </#list>
        </sql>

        <!-- 通用设置列 -->
        <sql id="${entity}SetColumns">
            <#list table.commonFields as field><#--生成公共字段-->
                <#if (field.propertyType!"") == "Date">
                    <if test="${field.propertyName}!=null">
                        ${field.name} = ${r"#{"}${field.propertyName}${r"}"},
                    </if>
                </#if>
                <#if (field.propertyType!"") != "Date">
                    <if test="${field.propertyName}!=null and ${field.propertyName}!=''">
                        ${field.name} = ${r"#{"}${field.propertyName}${r"}"},
                    </if>
                </#if>
            </#list>
            <#list table.fields as field>
                <#if !field.keyFlag><#--生成普通字段 -->
                    <#if (field.propertyType!"") == "Date">
                        <if test="${field.propertyName}!=null">
                            ${field.name} = ${r"#{"}${field.propertyName}${r"}"},
                        </if>
                    </#if>
                    <#if (field.propertyType!"")!= "Date">
                        <if test="${field.propertyName}!=null and ${field.propertyName}!=''">
                            ${field.name} = ${r"#{"}${field.propertyName}${r"}"},
                        </if>
                    </#if>
                </#if>
            </#list>
        </sql>
    </#if>

    <#if baseResultMap>
        <!-- 通用查询映射结果 -->
        <resultMap id="${entity}Map" type="${package.Entity}.${entity}">
            <#list table.fields as field>
                <#if field.keyFlag><#--生成主键排在第一位-->
                    <id column="${field.name}" property="${field.propertyName}"/>
                </#if>
            </#list>
            <#list table.commonFields as field><#--生成公共字段 -->
                <result column="${field.name}" property="${field.propertyName}"/>
            </#list>
            <#list table.fields as field>
                <#if !field.keyFlag><#--生成普通字段 -->
                    <result column="${field.name}" property="${field.propertyName}"/>
                </#if>
            </#list>
        </resultMap>
    </#if>

    <!-- 查询表${table.name}所有信息 -->
    <select id="findAll" resultMap="${entity}Map">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${table.name}
    </select>

    <#list table.fields as field>
        <#if field.keyFlag>
            <!-- 根据主键${field.propertyName}查询表${table.name}信息 -->
            <select id="getById" resultMap="${entity}Map">
                SELECT
                <include refid="Base_Column_List"/>
                FROM ${table.name}
                WHERE ${field.name}=${r"#{"}${field.propertyName}${r"}"}
            </select>
        </#if>
    </#list>

    <!-- 根据条件查询表${table.name}信息 -->
    <select id="getByCondition" resultMap="${entity}Map">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${table.name}
        <where>
            <include refid="${entity}ByCondition"/>
        </where>
    </select>

    <#list table.fields as field>
        <#if field.keyFlag>
            <!-- 根据主键${field.propertyName}删除表${table.name}信息 -->
            <delete id="deleteById">
                DELETE FROM
                ${table.name}
                WHERE ${field.name}=${r"#{"}${field.propertyName}${r"}"}
            </delete>
        </#if>
    </#list>

    <#list table.fields as field>
        <#if field.keyFlag>
            <!-- 根据主键${field.propertyName}更新表${table.name}信息 -->
            <update id="updateBySelective" parameterType="${package.Entity}.${entity}">
                UPDATE ${table.name}
                <set>
                    <include refid="${entity}SetColumns"/>
                </set>
                WHERE
                <#list table.fields as field><#if field.keyFlag>${field.name}=${r"#{"}${field.propertyName}${r"}"}</#if></#list>
            </update>
        </#if>
    </#list>

    <#list table.fields as field>
        <#if field.keyFlag>
            <!-- 新增表${table.name}信息 -->
            <insert id="insertBySelective">
                INSERT INTO ${table.name}
                <trim prefix="(" suffix=")" suffixOverrides=",">
                    <#list table.fields as field>
                        <#if (field.propertyType!"") == "Date">
                            <if test="${field.propertyName}!=null">
                                ${field.name} ,
                            </if>
                        </#if>
                        <#if (field.propertyType!"") != "Date">
                            <if test="${field.propertyName}!=null and ${field.propertyName}!=''">
                                ${field.name}  ,
                            </if>
                        </#if>
                    </#list>
                </trim>
                VALUES
                <trim prefix="(" suffix=")" suffixOverrides=",">
                    <#list table.fields as field>
                        <#if (field.propertyType!"") == "Date">
                            <if test="${field.propertyName}!=null">
                               ${r"#{"}${field.propertyName}${r"}"} ,
                            </if>
                        </#if>
                        <#if (field.propertyType!"") != "Date">
                            <if test="${field.propertyName}!=null and ${field.propertyName}!=''">
                               ${r"#{"}${field.propertyName}${r"}"} ,
                            </if>
                        </#if>
                    </#list>
                </trim>
            </insert>
        </#if>
    </#list>

    <!-- 分页查询${table.name}信息总数 -->
    <select id="selectCount" resultType="Long">
        SELECT count(1)
        FROM ${table.name}
        <where>
            <include refid="${entity}ByCondition"/>
        </where>
    </select>

<#--    <!-- mybatis mysql 分页查询${table.name}信息 &ndash;&gt;-->
<#--    <select id="selectList" resultMap="${entity}Map">-->
<#--        SELECT-->
<#--        <include refid="Base_Column_List"/>-->
<#--        FROM ${table.name}-->
<#--        <where>-->
<#--            <include refid="${entity}ByCondition"/>-->
<#--        </where>-->
<#--        <if test="limit!=null and limit >0">-->
<#--            limit ${r"${currentPage},${limit}"}-->
<#--        </if>-->
<#--    </select>-->


    <!-- mybatis oracle 分页查询${table.name}信息 -->
    <select id="selectList" resultMap="${entity}Map">
        select t.* from (
        SELECT rownum rn,
        <include refid="Base_Column_List"/>
        FROM   ${table.name}
        <where>
            <include refid="${entity}ByCondition"/>
        </where>
        ) t
        <if test="page>=1 and limit >=1">
        where rn  <![CDATA[ > ]]> ${r"(#{page}-1)*${limit} AND rn <![CDATA[ <=  ]]>#{page}*#{limit}"}
        </if>
    </select>
</mapper>
